package com.keeping.aiimagery.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.keeping.aiimagery.entity.OrdersServices;
import com.keeping.aiimagery.service.OrdersServicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author:zfx-t
 * @version:1.0
 */
@RestController
public class AiImageGenerateController {
    @Autowired(required = false)
    private OrdersServicesService ordersServicesService;

    @GetMapping("/image")
    public String getImage(String prompt,String userAccount) throws IOException {
        Map<String,String> data = new HashMap<>();
        data.put("prompt",prompt);
        String  body = HttpRequest.post("http://10.99.1.71:6007/sdapi/v1/txt2img")
                .header("Content-Type","application/json")
                .body(JSONUtil.toJsonStr(data))
                .execute()
                .body();
        JSONObject jsonObject = JSONUtil.parseObj(body);
        String image = (String) jsonObject.getJSONArray("images").get(0);
        if(image!=null){
            ordersServicesService.cut(userAccount,10,"AIImageGenerate");
            return image;
        }
        return null;

    }
}
